package com.fristvue.utils;

import java.text.Normalizer.Form;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * 工具类
 * @author
 */

@Component
public class RandomCode {
	/**
	 * 产生6个随机的字符, 用于验证用户的操作
	 */
	public char[] verifyCode() {
		String chars = "1234567890qwertyuiopasdfghjklzxcvbnm";
		char[] rands = new char[6];
		for(int i = 0; i < 6; i++) {
			int rand = (int) (Math.random() * 35);
            rands[i] = chars.charAt(rand);
		}
		return rands;
	}
	
	@Autowired
	private JavaMailSender javaMailSender;
	
	public String SendEmailVerifyCode(String email){
		String resultCode = String.valueOf(verifyCode());
		try {
			 SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
			 simpleMailMessage.setFrom("luoquan327@foxmail.com");
			 simpleMailMessage.setTo(email);
			 simpleMailMessage.setSubject("验证码");
			 simpleMailMessage.setText("尊敬的用户:<br/><hr>" +
	                    "你的邮箱验证代码为:<h3>" + resultCode + "</h3>请在120s内完成验证!请保护好您");
			 javaMailSender.send(simpleMailMessage);
		 }catch (Exception e) {
			// TODO: handle exception
			 e.printStackTrace();
		}
		return resultCode;
	 }
}
